package com.example.demo.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.BaseEntity;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @description：查询容器构建工具.
 * @author：lw
 * @create_time：2024/1/12
 **/
public class QueryWraperUtil {
    public static<T> QueryWrapper<T> getQueryWrap(T entity){
        QueryWrapper<T> queryWrapper=new QueryWrapper<>();
        Map<String,Object> map = BeanUtil.beanToMap(entity,false,true);
        List<String> excludeLikeFields=List.of("id","createTime","updateTime","startTime","endTime","isDeleted");
        if(map!=null && map.size()!=0){
            map.forEach((key, value) -> {
                // 表字段按驼峰设计。 StrUtil.toUnderlineCase():驼峰转下划线  ；StrUtil.toCamelCase():下划线转驼峰
                if (!excludeLikeFields.contains(key) && !NormalUtil.isEmpty(value))
                    queryWrapper.like(StrUtil.toUnderlineCase(key), value);
            });
            Set<String> keys=map.keySet();
            if(keys.contains("status") && map.get("status")!=null) queryWrapper.eq("status",map.get("status"));
            if(!NormalUtil.isEmpty(map.get("startTime")) && !NormalUtil.isEmpty(map.get("endTime"))) queryWrapper.ge("create_time",map.get("startTime")).le("create_time",map.get("endTime"));
        }
        queryWrapper.eq(BaseEntity.is_deleted_field,0);
        queryWrapper.orderByDesc("create_time");
        return  queryWrapper;
    }
    public static<T> QueryWrapper<T> getQueryWrap(T entity,List<String> excludeLikeFields){
        QueryWrapper<T> queryWrapper=new QueryWrapper<>();
        Map<String,Object> map =BeanUtil.beanToMap(entity,false,true);
        List<String> baseExcludeLikeFields=List.of("id","createTime","updateTime","startTime","endTime","isDeleted");
        excludeLikeFields.addAll(baseExcludeLikeFields);
        if(map!=null && map.size()!=0){
            map.forEach((key, value) -> {
                if (!excludeLikeFields.contains(key) && !NormalUtil.isEmpty(value))
                    queryWrapper.like(StrUtil.toUnderlineCase(key), value);
            });
            Set<String> keys=map.keySet();
            if(keys.contains("status") && map.get("status")!=null) queryWrapper.eq("status",map.get("status"));
            if(!NormalUtil.isEmpty(map.get("startTime")) && !NormalUtil.isEmpty(map.get("endTime"))) queryWrapper.ge("create_time",map.get("startTime")).le("create_time",map.get("endTime"));
        }
        queryWrapper.eq(BaseEntity.is_deleted_field,0);
        queryWrapper.orderByDesc("create_time");
        return  queryWrapper;
    }
    public static<T> QueryWrapper<T> getQueryWrap(T entity,Map<String,String> likeFieldsMap){
        // Map<String,String> likeFieldsMap=Map.of("a_b","aB","c_de","cDe");  表字段映射实体字段
        Map<String,Object> map =BeanUtil.beanToMap(entity,false,true);
        QueryWrapper<T> queryWrapper=new QueryWrapper<>();
        if(map!=null && map.size()!=0){
            likeFieldsMap.forEach((key, value) -> {
                if (!NormalUtil.isEmpty(map.get(value)))
                    queryWrapper.like(StrUtil.toUnderlineCase(key), map.get(value));
            });
            Set<String> keys=map.keySet();
            if(keys.contains("status") && map.get("status")!=null) queryWrapper.eq("status",map.get("status"));
            if(!NormalUtil.isEmpty(map.get("startTime")) && !NormalUtil.isEmpty(map.get("endTime"))) queryWrapper.ge("create_time",map.get("startTime")).le("create_time",map.get("endTime"));
        }
        queryWrapper.eq(BaseEntity.is_deleted_field,0);
        queryWrapper.orderByDesc("create_time");
        return  queryWrapper;
    }
}
